﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DataObjects.ADO;

namespace WindowsFormsApplication1
{
    public partial class form_Creport_TraTre : DevComponents.DotNetBar.Controls.SlidePanel
    {        
        SqlBaoCaoTraTre _sqlBC = new SqlBaoCaoTraTre();

        public form_Creport_TraTre()
        {
            InitializeComponent();
            loadComboboxNgayBC();
            cmbNgayBC.SelectedValueChanged += new EventHandler(cmbNgayBC_SelectedValueChanged);
        }

        public void UpdateSize()
        {
            this.Size = ((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size;
        }

        public void ShowSlide()
        {
            this.IsOpen = true;
            UpdateSize();
        }

        private void loadComboboxNgayBC()
        {
            DataTable table = _sqlBC.GetKey();
            cmbNgayBC.DataSource = table;
            cmbNgayBC.DisplayMember = table.Columns["Ngay"].ToString();
            cmbNgayBC.ValueMember = table.Columns["Ngay"].ToString();
            cmbNgayBC.Text = null;            
        }

        private void reLoadComboboxNgayBC()
        {
            cmbNgayBC.SelectedValueChanged -= cmbNgayBC_SelectedValueChanged;
            loadComboboxNgayBC();
            cmbNgayBC.SelectedValueChanged += cmbNgayBC_SelectedValueChanged;
        }

        private void cmbNgayBC_SelectedValueChanged(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table = _sqlBC.GetTableByDate(cmbNgayBC.Text);
            createCrystalReportSource(table);
        }

        private void butReport_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table = _sqlBC.GetTable();
            createCrystalReportSource(table);            
        }

        private void createCrystalReportSource(DataTable table)
        {
            string path = Application.StartupPath;
            path = path.Substring(0, path.LastIndexOf("bin"));

            table.TableName = "Creport_TraTre";
            table.WriteXmlSchema(path + @"\Creport_TraTre2.rpt");
            report.Load(path + @"\Creport_TraTre2.rpt");
            report.SetDataSource(table);
            crystalReportViewer.ReportSource = report;
            crystalReportViewer.Show();
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.IsOpen = false;
        }

        private void butSave_Click(object sender, EventArgs e)
        {            
            string result = _sqlBC.Insert();
            if (result != "Success")
                MessageBox.Show(result, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                reLoadComboboxNgayBC();
                MessageBox.Show("Đã xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Xóa báo cáo ngày " + cmbNgayBC.Text, "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
                    == DialogResult.Yes)
            {
                string result = _sqlBC.Delete(cmbNgayBC.Text);
                if (result != "Success")
                    MessageBox.Show(result,"Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
                else
                {
                    reLoadComboboxNgayBC();
                }
            }
        }
    }
}
